package com.younion.util;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.younion.util.config.ConfigHelper;

public class InterfaceRequestValidate {

	private String errorReason = "ERROR";

	/**
	 * 对请求进行验证是否合法
	 * 
	 * @param request
	 * @return
	 */
	public boolean validate(HttpServletRequest request) {
		ParametersUtils parametersUtils = new ParametersUtils();
		Map paramMap = parameterNoRepeatToMap(request);
		addParameter(parametersUtils, paramMap);
		String requestSign = request.getParameter("api_sign");
		String requestApiKey = request.getParameter("api_key");

		// 参数中没有api_key或没有值 非法请求
		if (org.apache.commons.lang.StringUtils.isEmpty(requestApiKey)) {
			setErrorReason("参数没有API_KEY");
			return false;
		} else if (!isExists(requestApiKey)) {// 是否存在
			setErrorReason("您的API_KEY不合法");
			return false;
		}
		if (org.apache.commons.lang.StringUtils.isEmpty(requestSign)) {
			setErrorReason("参数沒有API_SIGN");
			return false;
		}
		String local_apiSecret = ConfigHelper.fastGetConfig("interface", "secret");
		String encryptionSign = parametersUtils.getEncryptionSign(local_apiSecret);

		if (requestSign.equals(encryptionSign)) {
			return true;
		} else {
			setErrorReason("您的API_SIGN不合法");
			return false;
		}
	}

	/**
	 * 把request请求中的参数装入map- 请求中的参数名没有重复的情况
	 * 
	 * @param request
	 * @return
	 */
	private Map parameterNoRepeatToMap(HttpServletRequest request) {
		Map map = new HashMap();
		Enumeration enumratrion = request.getParameterNames();
		while (enumratrion.hasMoreElements()) {
			String paramName = (String) enumratrion.nextElement();
			String values = request.getParameter(paramName);
			map.put(paramName, values);
		}
		return map;
	}

	/**
	 * 把request请求参数装入需要认证的map中
	 * 
	 * @param parametersUtils
	 * @param paramMap
	 */
	private void addParameter(ParametersUtils parametersUtils, Map paramMap) {
		Iterator it = paramMap.entrySet().iterator();
		while (it.hasNext()) {// 只遍历一次,速度快
			Map.Entry e = (Map.Entry) it.next();
			// 如果key为api_sign 则不添加到需要加密的map中进行加密
			String key = (String) e.getKey();
			String value = (String) e.getValue();
			// System.out.println("key:" + key +" value:"+value);
			if ("api_sign".equals(key) || "keywords".equals(key))
				continue;
			parametersUtils.addStringParam(key, value);
		}
	}

	private boolean isExists(String apiKey) {
		String local_apiKey = ConfigHelper.fastGetConfig("interface", "apikey");
		System.out.println("apiKey:"+apiKey);
		System.out.println("local_apiKey:"+local_apiKey);
		if (StringUtils.isEmpty(local_apiKey) || !local_apiKey.equals(apiKey)) {
			return false;
		}
		return true;
	}

	public String getErrorReason() {
		return errorReason;
	}

	public void setErrorReason(String errorReason) {
		this.errorReason = errorReason;
	}

}
